package com.jinlejia.erp.adapter.prospect;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinlejia.erp.R;
import com.jinlejia.erp.utils.ViewHolderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王进 on 2019/1/7.
 */

public class UploadHouseTypeImageAdapter extends BaseAdapter {

    private Context mContext;
    public UploadHouseTypeImageAdapter(Context context){
        this.mContext = context;
    }
    private ArrayList<HouseTypeSource> sources = new ArrayList<>();

    @Override
    public int getCount() {
        return sources.size();
    }

    public void addSource(HouseTypeSource source){
        this.sources.add(source);
    }

    public void addAllSource(List<HouseTypeSource> sources){
        this.sources.clear();
        this.sources.addAll(sources);
    }

    public HouseTypeSource getSelectSource(){
        HouseTypeSource source = null;
        for (HouseTypeSource s:sources) {
            if (s.checked){
                source = new HouseTypeSource();
                source.setChecked(s.checked);
                source.setTypeInfo(s.typeInfo);
                source.setTempImageUrl(s.tempImageUrl);
                source.setImageUrl(s.imageUrl);
                break;
            }
        }
        return source;
    }

    @Override
    public HouseTypeSource getItem(int position) {
        return sources.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    private PreviewListener listener;

    public void setListener(PreviewListener listener) {
        this.listener = listener;
    }

    public void select(int position){
        for (int i = 0; i < sources.size(); i++) {
            HouseTypeSource source = sources.get(i);
            if (i==position){
                source.setChecked(true);
            }else {
                source.setChecked(false);
            }
        }
        notifyDataSetChanged();
    }

    private int mSelectedPosition = -1;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_prospect_upload_house_type_image, parent,false);
        }
        final HouseTypeSource source = sources.get(position);
        ImageView showImageView = ViewHolderUtil.get(convertView,R.id.showImageView);
        ImageView chooseFlagImage = ViewHolderUtil.get(convertView,R.id.chooseFlagImage);
        TextView houseInfoTextView = ViewHolderUtil.get(convertView,R.id.houseInfoTextView);
        View chooseImageLayout = ViewHolderUtil.get(convertView,R.id.chooseImageLayout);
        if (source.isChecked()){
            mSelectedPosition = position;
            chooseFlagImage.setImageResource(R.mipmap.circle_selected);
        }else {
            chooseFlagImage.setImageResource(R.mipmap.circle_normal);
        }
        Glide.with(mContext).load(source.imageUrl).placeholder(R.mipmap.ic_no_img).into(showImageView);
        houseInfoTextView.setText(source.getTypeInfo());
        showImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    ArrayList<HouseTypeSource> clone = (ArrayList<HouseTypeSource>) sources.clone();
                    listener.preview(mSelectedPosition,position,clone);
                }
            }
        });
        chooseFlagImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (HouseTypeSource s:sources) {
                    s.setChecked(false);
                }
                source.setChecked(true);
                notifyDataSetInvalidated();
            }
        });
        return convertView;
    }

    public void updateSource(String url, boolean check) {
        if (!check)return; //当用户没有选中时，直接返回
        for (HouseTypeSource s:sources) {
            if (s.imageUrl.equals(url)){
                s.setChecked(true);
            }
        }
    }

    public interface PreviewListener{
        void preview(int selectedPosition, int position, ArrayList<HouseTypeSource> sources);
    }
    public static final class HouseTypeSource implements Parcelable{
        private String imageUrl;
        private String typeInfo;
        private String tempImageUrl;
        private boolean checked;
        public HouseTypeSource(){

        }
        protected HouseTypeSource(Parcel in) {
            imageUrl = in.readString();
            typeInfo = in.readString();
            tempImageUrl = in.readString();
            checked = in.readByte() != 0;
        }

        public static final Creator<HouseTypeSource> CREATOR = new Creator<HouseTypeSource>() {
            @Override
            public HouseTypeSource createFromParcel(Parcel in) {
                return new HouseTypeSource(in);
            }

            @Override
            public HouseTypeSource[] newArray(int size) {
                return new HouseTypeSource[size];
            }
        };

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        public String getTempImageUrl() {
            return tempImageUrl;
        }

        public void setTempImageUrl(String tempImageUrl) {
            this.tempImageUrl = tempImageUrl;
        }

        public String getTypeInfo() {
            return typeInfo;
        }

        public void setTypeInfo(String typeInfo) {
            this.typeInfo = typeInfo;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(imageUrl);
            dest.writeString(typeInfo);
            dest.writeString(tempImageUrl);
            dest.writeByte((byte) (checked ? 1 : 0));
        }
    }
}
